package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.AddbankCard.Bank;
import com.banzhi.shop.entity.PersonDetail.Person;
import com.banzhi.shop.model.CallBack.AddBankCardCallBack;
import com.banzhi.shop.model.IAddbankCardModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by HT on 2017/5/3.
 * 绑定银行卡
 */

public class AddbankCardImpl implements IAddbankCardModel {

    @Override
    public void getBank(final AddBankCardCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.getBanksInfoUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("AddbankCardImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Bank bank = gson.fromJson(result, Bank.class);
                        callBack.ongetBankSuccess(bank.getRes());
                    } else {
                        callBack.ongetBankError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("AddbankCardImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(41)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void getData(final AddBankCardCallBack callBack) {
        final RequestParams params = new RequestParams(URLString.hostUrl + URLString.getUserInfoUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("AddbankCardImp2", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Person person = gson.fromJson(result, Person.class);
                        callBack.ongetDataSuccess(person.getUserInfo());
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("AddbankCardImp2", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(39)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void onsave(String bankname, String province, String city, String county, String street, String name, String cardno, String phone, String verify, final AddBankCardCallBack callBack) {
        final RequestParams params = new RequestParams(URLString.hostUrl + URLString.bingdingCardUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("bankname", bankname);
        params.addBodyParameter("province", province);
        params.addBodyParameter("city", city);
        params.addBodyParameter("county", county);
        params.addBodyParameter("street", street);
        params.addBodyParameter("name", name);
        params.addBodyParameter("cardno", cardno);
        params.addBodyParameter("phone", phone);
        params.addBodyParameter("verify", verify);
        LogUtils.d("AddbankCardImp3", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        callBack.onsaveSuccess(jsonobject);
                    } else {
                        callBack.onsaveError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("AddbankCardImp3", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(43)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
